package com.aaa.order.controller;

import com.aaa.entity.Order;
import com.aaa.entity.Product;
import com.aaa.order.service.IOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;
import java.util.Random;

@RestController
@RequestMapping("/order")
public class OrderController {


    //必须创建并交于spring容器管理。这样才能被注入到相应的类属性上
    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private IOrderService orderService;



    @GetMapping("/saveOrder")
    public String saveOrder(Long pid,Integer num){
        Order order=new Order();
        //用户得信息可以从登录后获取--Session  redis  jwt
        order.setUid(1);
        order.setUsername("张成");
        //为订单对象设置商品得信息
        order.setPid(pid);
        Product product = restTemplate.getForObject("http://shop-product/product/getById/"+pid, Product.class);
        order.setPname(product.getPname());
        order.setPprice(product.getPprice());
        order.setNumber(num);

        orderService.save(order);

        return "下单成功";
    }
}
